package com.tm.pojo;

import javax.persistence.Id;
import org.json.JSONObject;

import com.googlecode.objectify.annotation.Unindexed;

public class Instrument implements Comparable<Instrument> {
	@Id Long instrumentId;
	int term; //in days
	String strTerm;
	String name;
	String type;
	@Unindexed String definition;
	String bloombergId;
	String currency;
	@Unindexed String daycountConvention;
	@Unindexed int compoundingFrequency; //payments per year
	
	public Long getInstrumentId() {
		return instrumentId;
	}
	public int getTerm() {
		return term;
	}
	public String getStrTerm() {
		return strTerm;
	}
	public String getName() {
		return name;
	}
	public String getType() {
		return type;
	}
	public String getDefinition() {
		return definition;
	}
	public String getBloombergId() {
		return bloombergId;
	}
	public String getCurrency() {
		return currency;
	}
	public String getDaycountConvention() {
		return daycountConvention;
	}
	public int getCompoundingFrequency() {
		return compoundingFrequency;
	}
		
	public void setInstrumentId(Long instrumentId) {
		this.instrumentId = instrumentId;
	}
	public void setTerm(int term) {
		this.term = term;
	}
	public void setStrTerm(String strTerm) {
		try {
			this.strTerm = strTerm;
			if (strTerm.contains("w")) {
				strTerm = strTerm.substring(0, strTerm.indexOf("w"));
				term = Integer.parseInt(strTerm.trim())*7;
			} else if (strTerm.contains("m")) {
				strTerm = strTerm.substring(0, strTerm.indexOf("m"));
				term = Integer.parseInt(strTerm.trim())*30;
			} else if (strTerm.contains("y")) {
				strTerm = strTerm.substring(0, strTerm.indexOf("y"));
				term = Integer.parseInt(strTerm.trim())*360;
			} else {
				term = Integer.parseInt(strTerm.trim());
			}
		} catch (Throwable e) {
			term = 0;
		}
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setType(String type) {
		this.type = type;
	}
	public void setDefinition(String definition) {
		this.definition = definition;
	}
	public void setBloombergId(String bloombergId) {
		this.bloombergId = bloombergId;
	}
	public void setCurrency(String currency) {
		this.currency = currency;
	}
	public void setDaycountConvention(String daycountConvention) {
		this.daycountConvention = daycountConvention;
	}
	public void setCompoundingFrequency(int compoundingFrequency) {
		this.compoundingFrequency = compoundingFrequency;
	}
	public void setCompoundingFrequency(String compoundingFrequency) {
		this.compoundingFrequency = 1;
		try {
			this.compoundingFrequency = Integer.parseInt(compoundingFrequency.trim());
		} catch (Throwable e) {
			//ignore
		}
	}

	public int compareTo(Instrument o) {
		return this.term - ((Instrument) o).getTerm();
	}
	@SuppressWarnings("finally")
	public JSONObject toJSONObject() {
		JSONObject json = new JSONObject();
		try { 
			json.put("instrumentId", instrumentId);
			json.put("term", term);
			json.put("strTerm", strTerm);
			json.put("name", name);
			json.put("type", type);
			json.put("definition", definition);
			json.put("bloombergId", bloombergId);
			json.put("currency", currency);
			json.put("daycountConvention", daycountConvention);
			json.put("compoundingFrequency", compoundingFrequency);
		} catch (Throwable e) {
			
		} finally {
			return json;
		}
	}
	@Override
	public boolean equals(Object obj) {
		return this.instrumentId == ((Instrument)obj).getInstrumentId();
	}
	
}
